package com.nanbei.rabbit.consumer.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/8/28 15:34
 */
@Component
public class DirectConsumerListener {

    //使用bean绑定交换机和队列
/*    @RabbitListener(queues = "direct_queue_01")
    public void listener01(Message message){  //message 对象封装消息信息
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        byte[] body = message.getBody();  // 获取的消息内容
        String msg = new String(body);
        System.out.println("direct_queue_01:"+receivedRoutingKey+"  "+msg);
    }

    @RabbitListener(queues = "direct_queue_02")
    public void listener02(Message message){  //message 对象封装消息信息
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        byte[] body = message.getBody();  // 获取的消息内容
        String msg = new String(body);

        System.out.println("direct_queue_02:"+receivedRoutingKey+"  "+msg);
    }*/


    // 使用注解绑定交换机和队列
    @RabbitListener(bindings = {
            @QueueBinding(
                  value =  @Queue(name = "direct_queue_01"),
                    exchange = @Exchange(name = "direct_exchange"),
                    key = {"info","error"}

            )
    })
    public void listener01(Message message){  //message 对象封装消息信息
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        byte[] body = message.getBody();  // 获取的消息内容
        String msg = new String(body);
        System.out.println("direct_queue_01:"+receivedRoutingKey+"  "+msg);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value =  @Queue(name = "direct_queue_02"  ),
                    exchange = @Exchange(name = "direct_exchange" ),
                    key = {"error"}

            )
    })
    public void listener02(Message message) {  //message 对象封装消息信息
        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
        byte[] body = message.getBody();  // 获取的消息内容
        String msg = new String(body);

        System.out.println("direct_queue_02:" + receivedRoutingKey + "  " + msg);
    }
}
